
package com.rt.schedulenew.utils.util;

import net.sf.ehcache.CacheException;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.List;
import net.sf.ehcache.Element;
import net.sf.ehcache.Cache;
import java.io.Serializable;
import java.util.Objects;
import net.sf.ehcache.CacheManager;
import org.slf4j.Logger;

public class EhCacheUtil {
    private static final Logger logger = LoggerFactory.getLogger(EhCacheUtil.class);
    private static CacheManager manager = null;

    static {
        try {
            // 根据配置文件加载缓存配置规则
            manager = CacheManager.create(EhCacheUtil.class.getClassLoader().getResourceAsStream("ehcache.xml"));
        } catch (CacheException e) {
            logger.error("获取ehcache.xml失败", e.getMessage());
        }
    }

    public static <T extends Serializable> boolean isExpired(String cacheName, String key, T value) {
        Cache cache = checkCache(cacheName);
        Element e = cache.getQuiet(key);
        return e != null;
    }

    public static <T extends Serializable> void put(String cacheName, String key, T value) {
        Cache cache = checkCache(cacheName);
        Element e = new Element(key, value);
        cache.put(e);
    }

    public static <T extends Serializable> void putList(String cacheName, String key, List<T> value) {
        Cache cache = checkCache(cacheName);
        Element e = new Element(key, value);
        cache.put(e);
    }

    public static <T extends Serializable> void put(String cacheName, String key, T value, boolean eternal) {
        Cache cache = checkCache(cacheName);
        Element element = new Element(key, value);
        element.setEternal(eternal);
        cache.put(element);
    }

    public static <T extends Serializable> void put(String cacheName, String key, T value, int timeToLiveSeconds, int timeToIdleSeconds) {
        Cache cache = checkCache(cacheName);
        Element element = new Element(key, value);
        element.setTimeToLive(timeToLiveSeconds);
        element.setTimeToIdle(timeToIdleSeconds);
        cache.put(element);
        cache.flush();
    }

    public static Object get(String cacheName, String key) {
        try {
            Cache cache = checkCache(cacheName);
            Element e = cache.get(key);
            if (Objects.nonNull(e)) {
                return e.getObjectValue();
            }
            return null;
        } catch (IllegalStateException | CacheException ex) {
            logger.error(ex.getMessage(), ex);
            return null;
        }
    }

    public static <T> T getValue(String cacheName, String key) {
        try {
            Cache cache = checkCache(cacheName);
            Element e = cache.get(key);
            if (Objects.nonNull(e)) {
                Object value = e.getObjectValue();
                if (Objects.nonNull(value)) {
                    return (T) value;
                }
            }
            return null;
        } catch (IllegalStateException | CacheException ex) {
            logger.error(ex.getMessage(), ex);
            return null;
        }
    }

    public static void remove(String cacheName, String key) {
        Cache cache = checkCache(cacheName);
        cache.remove(key);
    }

    public static void removeAll(String cacheName, Collection<String> keys) {
        Cache cache = checkCache(cacheName);
        cache.removeAll(Boolean.TRUE);
    }

    public static void removeForCacheName(String cacheName) {
        manager.clearAllStartingWith(cacheName);
    }

    public static void clearAll() {
        manager.clearAll();
    }

    private static Cache checkCache(String cacheName) {
        Cache cache = EhCacheUtil.manager.getCache(cacheName);
        if (null == cache) {
            throw new IllegalArgumentException("name=[" + cacheName + "],不存在对应的缓存组,请查看ehcache.xml");
        }
        return cache;
    }

    public static void main(String[] args) {
        CacheManager cacheManager = new CacheManager();
        Cache cache = cacheManager.getCache("helloworld");
        String key = "greeting";
        Element putGreeting = new Element("greeting", "Hello, World!");
        cache.put(putGreeting);
        Element getGreeting = cache.get("greeting");
        logger.info(String.valueOf(getGreeting.getObjectValue()));
    }
}
